package com.dewod.producesales.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ObjectUtils;
import com.dewod.producesales.R;
import com.dewod.producesales.activity.DeliveryDetailActivity;
import com.dewod.producesales.model.response.StoreSalesContract;
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.client.utils.DateUtils;

import java.util.List;

/**
 * 发货适配器
 */
public class DeliveryAdapter  extends RecyclerView.Adapter<DeliveryAdapter.ViewHolder> {

    private List<StoreSalesContract> salesContracts;

    private Context context;

    public DeliveryAdapter(List<StoreSalesContract> deliveryItems) {
        this.salesContracts = deliveryItems;
    }

    public void updateData(List<StoreSalesContract> deliveryItems) {
        this.salesContracts = deliveryItems;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_delivery, parent, false);
        context = parent.getContext();
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        StoreSalesContract item = salesContracts.get(position);
        holder.contractNumber.setText(item.getSalesContractNo());
        holder.manufacturerName.setText(item.getDeviceSupplier());
        holder.estimatedShippingDate.setText(item.getExpectedShipDate() == null ? "未设置预计发货时间" : DateUtils.formatDate(item.getExpectedShipDate(), "yyyy-MM-dd"));
        holder.shippingStatus.setText(ObjectUtils.equals(0 , item.getLogisticsStatus())  ?  "待发货" : "已发货");

        holder.itemView.setOnClickListener(v -> {
            // 跳转到发货详情页面
            Intent intent = new Intent(context, DeliveryDetailActivity.class);
            intent.putExtra("contract", JSON.toJSONString(item));
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return salesContracts.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        // 合同号
        TextView contractNumber;

        // 供应商名称
        TextView manufacturerName;
        // 预计发货时间
        TextView estimatedShippingDate;
        // 发货状态
        TextView shippingStatus;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            contractNumber = itemView.findViewById(R.id.contract_number);
            manufacturerName = itemView.findViewById(R.id.manufacturer_name);
            estimatedShippingDate = itemView.findViewById(R.id.estimated_shipping_date);
            shippingStatus = itemView.findViewById(R.id.shipping_status);
        }
    }
}
